package com.rmb.common.model.sensitive;

import com.rmb.common.model.assist.AssistQuery;
import com.rmb.common.model.state.TaskStatus;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0
 * @Author ZhaoMuYang
 * @Date 2021/1/13 15:32
 */
public class IdDesensitizationUtil {
    private static final Pattern CHINESE = Pattern.compile("[\u4e00-\u9fa5]");
    private static final Pattern FIGURE=Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
    /**
     * 真实姓名：只展示姓名第一个字（中文）
     * 账户密码：使用*代替
     * 存储资产：留取第第一位数字 其后￥代替
     * 身份证号：年月日脱敏，前6后4不脱敏
     * 手机号：前三后四不脱敏，中间脱敏
     * 银行卡：前6位和后4位不脱敏，中间脱敏
     */
    public static String replaceStr(String param) {
        if(StringUtils.isEmpty(param)){
            return "";
        }
        int len=param.length();
        if(isContainChinese(param)){
            return param.replaceAll("(.{1})(.*)(.{0})", "$1" + "***");
        }
        if(param.contains(AssistQuery.ENCODE_DOLLAR.getContent())
                || param.contains(AssistQuery.ENCOD_MAKE.getContent())
                || param.contains(AssistQuery.ENCODE_UNDERLINE.getContent())){
            return param.replace(param,"***********");
        }


        if(isMatch(param)){
            return param.replaceAll(param.substring(TaskStatus.NOT_IMPLEMENTED_STATUS.getStatus()), String.valueOf(AssistQuery.ENCOD_CNY));
        }

        return param.replaceAll("(.{"+(len<12?3:6)+"})(.*)(.{4})", "$2" +"****" + "$3");
    }

    /**
     判断是否含中文
     @param str 传入要脱敏的字符串
     @return 是/否 true/false
     */
    public static boolean isContainChinese(String str) {
        Matcher m = CHINESE.matcher(str);
        return m.find();
    }

    /**
     * 判断是否是金额
     */
    public static boolean isMatch(String str){
        Matcher matcher = FIGURE.matcher(str);
        return  matcher.find();
    }
}
